#!/bin/bash

# 一主二备拓扑状态检查脚本

SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SOURCE_DIR/config.conf"

echo "=== 一主二备拓扑状态 ==="
echo

# 定义节点
NODES=("${VM_NAME_PREFIX}-master1" "${VM_NAME_PREFIX}-worker1" "${VM_NAME_PREFIX}-worker2")

echo "虚拟机状态:"
printf "%-20s %-10s %-15s %-10s %s\n" "虚拟机名称" "类型" "IP地址" "状态" "VNC端口"
echo "------------------------------------------------------------"

for vm_name in "${NODES[@]}"; do
    info_file="$VM_IMAGE_DIR/${vm_name}.info"
    pid_file="$VM_IMAGE_DIR/${vm_name}.pid"
    
    if [ -f "$info_file" ]; then
        vm_type=$(grep "VM_TYPE=" "$info_file" | cut -d'=' -f2)
        vm_ip=$(grep "VM_IP=" "$info_file" | cut -d'=' -f2)
        vnc_port=$(grep "VNC_PORT=" "$info_file" | cut -d'=' -f2)
        
        if [ -f "$pid_file" ] && kill -0 "$(cat "$pid_file")" 2>/dev/null; then
            status="运行中"
        else
            status="已停止"
        fi
        
        printf "%-20s %-10s %-15s %-10s %s\n" "$vm_name" "$vm_type" "$vm_ip" "$status" "$vnc_port"
    else
        printf "%-20s %-10s %-15s %-10s %s\n" "$vm_name" "未知" "未知" "未创建" "未知"
    fi
done

echo
echo "网络状态:"
if ip link show "$BRIDGE_NAME" >/dev/null 2>&1; then
    echo "✓ 网桥 $BRIDGE_NAME 已配置"
    ip addr show "$BRIDGE_NAME" | grep inet | head -1
else
    echo "✗ 网桥 $BRIDGE_NAME 未配置"
fi

echo
echo "可用操作:"
echo "- 批量安装: $SOURCE_DIR/install-all-1m2w.sh"
echo "- 批量启动: $SOURCE_DIR/start-all-1m2w.sh"
echo "- 批量停止: $SOURCE_DIR/stop-all-1m2w.sh"
echo "- 状态检查: $SOURCE_DIR/status-1m2w.sh"
